package com.chtw.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chtw.entity.LoginAccount;
import com.chtw.entity.StudentInfo;
import com.chtw.entity.TeacherInfo;
import com.chtw.service.LoginAccountService;
import com.chtw.service.StudentService;
import com.chtw.service.TeacherService;
import com.chtw.utils.Result;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/account")
public class LoginAccountController {

	@Autowired
	private LoginAccountService accountService;
	
	@Autowired
	private StudentService stuService;
	
	@Autowired
	private TeacherService teacherService;
	
	@RequestMapping("/Login")
	@ResponseBody
	public Result Login(LoginAccount account,HttpSession session) {
		//System.out.println(account.getAccount()+"--"+account.getPassword());
		LoginAccount account2 = accountService.login(account);
		if(account2!=null) {
			session.setAttribute("currentUser", account2.getAccount());
			session.setAttribute("accountLevel", account2.getAccountLevel());
			if(account2.getAccountLevel()==1) {
				System.out.println("教师登录");
				session.setAttribute("tcurrentUser", account2.getAccount());
				TeacherInfo teacherInfo = teacherService.getinfo(Integer.parseInt(account2.getAccount()));
				session.setAttribute("tUser", teacherInfo.getTeachName());
				PageInfo<StudentInfo> info = stuService.page(1);
				PageInfo<TeacherInfo> tinfo = teacherService.page(1);
				session.setAttribute("nowpagem", info.getPageNum());
				session.setAttribute("pages", info.getPages());
				session.setAttribute("total", info.getTotal());
				return Result.seccessTeach(account2);
			}else if(account2.getAccountLevel()==0) {
				System.out.println("管理员登录");
				session.setAttribute("aUser", account2.getAccount());
				PageInfo<StudentInfo> info = stuService.page(1);
				PageInfo<TeacherInfo> tinfo = teacherService.page(1);
				//List<StudentInfo> sList = info.getList();
				//if(sList!=null) {
					session.setAttribute("nowpagem", info.getPageNum());
					session.setAttribute("pages", info.getPages());
					session.setAttribute("tpages", tinfo.getPages());
					//session.setAttribute("sList", sList);
					session.setAttribute("total", info.getTotal());
				//}
				return Result.seccessAdmin(account2);
			}else if(account2.getAccountLevel()==2) {
				session.setAttribute("scurrentUser", account2.getAccount());
				System.out.println("学生登录");
				session.setAttribute("sUser", stuService.getinfo(Integer.parseInt(account.getAccount())));
				return Result.seccessStu(account2);
			}
		}
		return Result.fail("用户名或密码错误！");
	}
	
	@RequestMapping("/checkAccount")
	@ResponseBody
	public Result checkAccount(LoginAccount account) {
		LoginAccount account2 = accountService.checkAccount(account);
		if(account2!=null) {
			if(account2.getAccountLevel()==1) {
				//System.out.println("教师");
				return Result.seccessTeach(account2);
			}else if(account2.getAccountLevel()==0) {
				//System.out.println("管理员");
				return Result.seccessAdmin(account2);
			}else if(account2.getAccountLevel()==2) {
				//System.out.println("学生");
				return Result.seccessStu(account2);
			}
		}
		return Result.fail("该账号不存在");
	}
	
	/**
	 * 修改密码是检查原密码
	 * @param account
	 * @return
	 */
	@RequestMapping("/checkPassword")
	@ResponseBody
	public Result checkPassword(LoginAccount account) {
		LoginAccount account2 = accountService.checkPassword(account);
		if(account2!=null) {
			return Result.seccess();
		}else{
			return Result.fail("原密码错误");
		}
	}
	
	/**
	 * 修改用户密码
	 * @param account
	 * @return
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public Result updatePassword(LoginAccount account) {
		int i = accountService.updatePassword(account);
		if(i>0) {
			return Result.seccess();
		}else {
			return Result.fail("修改失败！");
		}
	}
	
	@RequestMapping("/geusername")
	@ResponseBody
	public Result geusername(HttpSession session,String t) {
		if(t.equals("t")) {
			return Result.seccess(session.getAttribute("tUser"));
		}else if(t.equals("a")) {
			return Result.seccess(session.getAttribute("aUser"));
		}else if(t.equals("s")) {
			System.out.println("session.getAttribute(\"sUser\")"+session.getAttribute("sUser"));
			return Result.seccess(session.getAttribute("sUser"));
		}else {
			return Result.fail("获取失败！");
		}
		
	}
}
